﻿using UnityEngine;
using System.Collections;
namespace Room
{
    public class RCAIArea : RCAIBase
    {

        public RCAIArea(RoomCharactor owner)
            : base(owner)
        {
            type = AIType.Area;
        }

        //public delegate void EventEnterAreaHandler(string areaId);

        //public static EventEnterAreaHandler eventEnterAreaHandler;

        public float areaRange = 1.0f;
        public bool openPage;
        public string pageName;
        public string pageOption;
        public Vector3 resetPosition;

        private bool isOut;

        public void Init(float areaRange, string pageName, string pageOption, Vector3 resetPosition)
        {
            this.areaRange = areaRange;
            this.openPage = !string.IsNullOrEmpty(pageName);
            this.pageName = pageName;
            this.pageOption = pageOption;
            this.resetPosition = resetPosition;
        }

        public override void Tick()
        {
            var self = owner.charactorManager.GetSelfCharactor();



            if (self == null)
                return;
            var ai = self.ai as RCAISelf;
            if (ai.AutoPathFinding && ai.autoClickCharactor != owner)
            {
                return;
            }
            // var selfAI = self.ai as RCAISelf;
            var delta = self.transform.position - owner.transform.position;
            delta.y = 0;
            if (delta.magnitude < areaRange)
            {
                if (isOut)
                {
                    isOut = false;
                    if (openPage)
                    {
                        owner.charactorManager.room.uiFuncs += OpenPage;
                    }
                    else
                    {
                        DataMessageTool.AddMessage(DataMessage.AREA_ENTER, owner);
                    }
                }
                if (openPage)
                {
                    var dir = self.transform.position - owner.transform.position;
                    dir.y = 0f;
                    resetPosition = owner.transform.position + dir.normalized * (areaRange + 1f);
                    self.transform.position = resetPosition;
                    self.StopMove();
                }
            }
            else
            {
                isOut = true;
            }

        }
        public override void DoResume()
        {
            if(openPage)
            {
                //主城传送门在一开始玩家在里面的时候不打开界面
                isOut = false;
            }
            else
            {
                isOut = true;
            }
        }

        private void OpenPage()
        {

            //if (pageName.Equals("UICopy"))
            //{
            //    var curStory = StaticData.GetCurStory();
            //    var curQuest = Dict.DictDataManager.Instance.tableStoryQuestDao.GetById(curStory.questId);
            //    pageOption = "levelId=" + curQuest.missionId;
            //}
            UI.PageManager.Instance.OpenPage(pageName, pageOption);
            var self = owner.charactorManager.GetSelfCharactor();
            self.transform.position = resetPosition;
            self.StopTo(resetPosition);
        }

    }

}

